from app.models.bazi import *
from app.models.nameList import *
from app.controller.Naming import Naming
from app.controller.data import unused_word
from app import app,jsonify,request
from xpinyin import Pinyin
from app.controller.NameTest import NameTest

#八字起名
class BzNaming(Naming):
    def __init__(self,date_s,time_s,surname,sex,pagenow=1):
        Naming.__init__(self,date_s,time_s)
        self.surname = surname
        self.sex = sex
        self.pagenow = pagenow
    wx_nexus = {
        '金':{
            'sheng':'土',
            'ke':'木'
        },
        '木': {
            'sheng': '木',
            'ke': '水'
        },
        '水': {
            'sheng': '金',
            'ke': '土'
        },
        '火': {
            'sheng': '木',
            'ke': '水'
        },
        '土': {
            'sheng': '木',
            'ke': '木'
        }
    }
    #根据五行喜好起名
    def wx_qm(self,xh_hanzi):
        #根据喜好 找出N个该属性汉字
        page = 50
        num = (int(self.pagenow)-1)*page
        wx_hanzi_arr = db.session.query(Wxhanzi.hanzi).filter_by(wx=xh_hanzi).offset(num).limit(page)
        name_arr = []
        for item in wx_hanzi_arr:
            if not item.hanzi in unused_word:
                name_arr.append({
                    'wx_word':item.hanzi,
                    'name':db.session.query(NameList.name_word).filter(NameList.name_word.like('%'+item.hanzi+'%')).first().name_word
                })
        return name_arr
    #根据字典查询 获取五行及含义
    def get_menu(self,name):
        name_arr = list(map(str, name))
        result_arr = []
        try:
            for item in name_arr:
                mean = db.session.query(ZiDian.jianjie).filter_by(hanzi=item).first().jianjie.split('<br>')
                mean_str = '%s：%s %s' % (mean[0],mean[2],mean[3])
                result_arr.append({
                        'hz':item,
                        'mean':mean_str
                    })
        except:
            pass
        return result_arr
    #获取八字 五行 名字集合 描述 总调用方法
    def sun_func(self):
        bazi_result = self.bazi()
        bz = bazi_result['bazi_arr']
        wx = self.wuxing(bz)
        wx_fx = self.wx_fx(bz,wx)
        name_arr = self.wx_qm(wx_fx['xihao'])
        name_mean_wx = []
        pin = Pinyin()
        for name in name_arr:
            name_word = name['name']
            nmTest = NameTest(self.surname+name_word,self.sex)
            name_mean_wx.append({
                'name':name_word,
                'wx_word':name['wx_word'],
                'pinyin':pin.get_pinyin(name_word),
                'mean':self.get_menu(name_word),
                'score':int(nmTest.count_score())
            })
        return {
            'bz':bz,
            'wx':wx,
            'name_arr':name_mean_wx,
            'describe':wx_fx['describe'],
            'wx_arr':wx_fx['wx_arr'],
            'wx_xihao':wx_fx['xihao'],
            'yl':bazi_result['yl'],
            'shu':bazi_result['shu'],
            'sex':self.sex,
            'surname':self.surname
        }
 






